package scau.online.web.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.Goods;
import scau.cty.service.IGoodsService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by caoxiong on 2017/4/11.
 * 管理员 商品增删查改管理器
 */
@Api(value = "商品信息管理控制器",description = "商品信息接口")
@RestController
@RequestMapping(value = "/admin_goods",produces = "application/json")
public class AdminGoodsController extends BaseController{
    @Autowired
    private IBaseService<Goods> goodsIBaseService;
    @Autowired
    private IGoodsService iGoodsService;
    /**
     * 商品列表  根据pageSize和pageNow查询商品 order为排序方式
     * @param pageSize
     * @param pageNow
     * @param order
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "商品列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "排序方式，不传参数或参数为0为默认排序 1优先级排序，2人气排序，3销量排序，4综合排序，5价格降序排序",
                    required = true,paramType = "query")})
    @GetMapping("/goods_list")
    public String getGoodsList(Integer pageSize,Integer pageNow,Integer order) throws Exception{
        Map map = new LinkedHashMap<>();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        List<Goods> goodsList = new ArrayList<>();
        if(order == null || order == 0){
            //默认排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"id");
        }else if(order == 1){ //优先级排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"priority");
        }else if(order == 2){ //人气排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"click_count");
        }else if(order == 3){ //销量排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"sale_count");
        }else if(order == 4){ //综合排序
            goodsList = iGoodsService.selectOrderBySaleAndClick(pageNow,pageSize);
        }else{ //价格降序排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"price");
        }
        map.put("goodsList",goodsList);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
    /**
     * 根据商品 id 查询该商品的所有字段
     * @param id
     * @return
     */
    @ApiOperation(value = "商品详情接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "商品唯一标识id",required = true,paramType = "query")})
    @GetMapping(value = "/goods_select")
    public String findGoodsById(String id){
        Map map = new LinkedHashMap();
        if (id == null || id == "") {
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        Goods goods = goodsIBaseService.findByKey(id);
        map.put("goods", goods);
        sendCode(map, Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
    /**
     * 根据指定商品 id 删除商品
     * @param id
     * @return
     */
    @ApiOperation(value = "删除商品接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "商品唯一标识id",required = true,paramType = "query")})
    @PostMapping(value = "/goods_delete")
    public String deleteGoodsById(String id){
        Map map = new LinkedHashMap();
        if(id == null || id == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int deleteGoods = goodsIBaseService.delete(id);
        map.put("deleteGoods",deleteGoods);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
    /**
     * 添加商品
     *  title
     *  price
     *  brand
     *  type
     *  inventory
     *  saleCount
     *  clickCount
     *  thumbnail
     *  picture
     *  priority
     * @return
     */
    @ApiOperation(value = "增加商品接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "title",value = "商品名称",required = true,paramType = "query"),
            @ApiImplicitParam(name = "price",value = "商品价格",required = true,paramType = "query"),
            @ApiImplicitParam(name = "brand",value = "商品品牌",required = true,paramType = "query"),
            @ApiImplicitParam(name = "type",value = "商品类型",required = true,paramType = "query"),
            @ApiImplicitParam(name = "inventory",value = "商品库存",required = true,paramType = "query"),
            @ApiImplicitParam(name = "saleCount",value = "商品销售量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "clickCount",value = "商品点击量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "thumbnail",value = "商品缩略图",required = true,paramType = "query"),
            @ApiImplicitParam(name = "picture",value = "商品图片",required = true,paramType = "query"),
            @ApiImplicitParam(name = "priority",value = "商品优先级",required = true,paramType = "query")})
    @PostMapping(value = "/goods_add")
    public String addGoods(Goods goods){
        Map map = new LinkedHashMap();
        if(goods.getTitle() == null || goods.getTitle() == "" || goods.getType() == null || goods.getType() == "" ||
                goods.getThumbnail() == null || goods.getThumbnail() == "" || goods.getPicture() == null || goods.getPicture() == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(goods.getPrice() == null ){
            goods.setPrice(new BigDecimal(0));
        }
        if(goods.getBrand() == null){
            goods.setBrand("");
        }
        if(goods.getInventory() == null){
            goods.setInventory(0);
        }
        if(goods.getSaleCount() == null){
           goods.setSaleCount(0);
        }
        if(goods.getClickCount() == null){
            goods.setClickCount(0);
        }
        if(goods.getPriority() == null ){
            goods.setPriority(0);
        }
        int addGoods = goodsIBaseService.add(goods);
        map.put("addGoods",addGoods);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
    /**
     * 修改商品信息 通过商品唯一id 标识修改
     *  id
     *  title
     *  price
     *  brand
     *  type
     *  inventory
     *  sale_count
     *  click_count
     *  thumbnail
     *  picture
     *  priority
     * @return
     */
    @ApiOperation(value = "修改商品信息接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "商品唯一标识id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "title",value = "商品名称",required = true,paramType = "query"),
            @ApiImplicitParam(name = "price",value = "商品价格",required = true,paramType = "query"),
            @ApiImplicitParam(name = "brand",value = "商品品牌",required = true,paramType = "query"),
            @ApiImplicitParam(name = "type",value = "商品类型",required = true,paramType = "query"),
            @ApiImplicitParam(name = "inventory",value = "商品库存",required = true,paramType = "query"),
            @ApiImplicitParam(name = "saleCount",value = "商品销售量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "clickCount",value = "商品点击量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "thumbnail",value = "商品缩略图",required = true,paramType = "query"),
            @ApiImplicitParam(name = "picture",value = "商品图片",required = true,paramType = "query"),
            @ApiImplicitParam(name = "priority",value = "商品优先级",required = true,paramType = "query")})
    @PostMapping(value = "/goods_update")
    public String updateGoodsById(Goods goods){
        Map map = new LinkedHashMap();
        if(goods.getId() == null || goods.getId() == "" ){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        //参数全部为空时返回参数错误
        if(goods.getBrand()==null && goods.getPriority()==null && goods.getPicture()==null && goods.getType()== null
                && goods.getPrice() == null && goods.getTitle() == null && goods.getThumbnail() == null
                && goods.getSaleCount()==null && goods.getClickCount() == null && goods.getInventory()==null){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int updateGoods = goodsIBaseService.updateNotNull(goods);
        map.put("updateGoods",updateGoods);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
